unit Unit5; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb, Unit1,
  db, DBGrids, Menus, Unit6, Unit7, LR_DBSet, LR_Class;

type

  { TForm5 }

  TForm5 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow;
    procedure MenuItem9Click(Sender: TObject);
    procedure ordenar_grid();
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
  private
    nombre : string;
  public
    { public declarations }
  end; 

var
  Form5: TForm5; 

implementation

{ TForm5 }

procedure TForm5.ordenar_grid();
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
  SQLQuery1.SQL.Text := 'select * from fabricante where fab_nombre ilike ''%' + nombre
    + '%'' order by fab_nombre';

  SQLQuery1.Open;

  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Nombre';
  DBGrid1.Columns[2].Title.Caption := 'Descripción';

  DBGrid1.Columns[0].Width := 85;
  DBGrid1.Columns[1].Width := 200;
  DBGrid1.Columns[2].Width := 295;
end;

procedure TForm5.MenuItem2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm5.MenuItem4Click(Sender: TObject);
begin
  Form6.ShowModal;
  FormShow;
end;

procedure TForm5.MenuItem5Click(Sender: TObject);
var
  codigo : String;
  QueryString : String;
  
begin
  codigo := InputBox('Baja de Fabricante', 'Ingrese el código de fabricante a eliminar:', '');
  
  if codigo <> '' then
  begin
    QueryString := 'delete from fabricante where fab_cdg = ' + codigo;
    
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
    
    ShowMessage('BAJA EXITOSA');
  end;
  
  FormShow;
end;

procedure TForm5.MenuItem6Click(Sender: TObject);
begin
  ShowMessage('Click sobre el Fabricante a modificar...');
end;

procedure TForm5.MenuItem8Click(Sender: TObject);
begin
  nombre := InputBox('Filtro de Fabricantes por Nombre', 'Ingrese parte del nombre del fabricante:', '');
  
  if nombre <> '' then
    ordenar_grid;
end;

procedure TForm5.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;

  nombre := '%';
  
  ordenar_grid;
end;

procedure TForm5.MenuItem9Click(Sender: TObject);
begin
  frReport1.LoadFromFile('listado_fabricantes.lrf');
  frReport1.ShowReport;
end;

procedure TForm5.MenuItem10Click(Sender: TObject);
begin
  FormShow;
end;

procedure TForm5.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm5.DBGrid1CellClick(Column: TColumn);
begin
  Form7.codigo := SQLQuery1.FieldByName('fab_cdg').AsString;
  Form7.ShowModal;
  
  ordenar_grid;
end;

initialization
  {$I unit5.lrs}

end.

